var user = JSON.parse(localStorage.getItem("user"));
var pageIndex;
var pageSize;
var searchName = '';
// 获取所有数据和总记录数 { total: 100, data: [...] }
var dataResult = null;
mini.parse();
var grid = mini.get('archivesGrid');


function getArchivesList(pageIndex, pageSize, searchName) {
    $.ajax({
        url: GatewayUrl() + '/archives/list/' + user.uuid,
        type: 'get',
        async: false,
        data: {
            "pageIndex": pageIndex,
            "pageSize": pageSize,
            "searchName": searchName
        },
        headers: {
            "Authorization": localStorage.getItem("token")
        },
        success: function (dt) {
            if (dt.code === 200) {
                dataResult = mini.decode(dt.data);
            }
        }
    })
}

$(function () {
    pageIndex = 0;
    pageSize = 6;
    getArchivesList(pageIndex + 1, pageSize, searchName);
    fillData(0, grid.getPageSize(), dataResult, grid);
})

// 分页填充细节处理
function fillData(pageIndex, pageSize, dataResult, grid) {

    var data = dataResult.data, totalCount = dataResult.total;

    var arr = [];
    var start = pageIndex * pageSize, end = start + pageSize;
    for (var i = 0; i < dataResult.data.length; i++) {
        arr.push(dataResult.data[i]);
    }


    grid.setTotalCount(totalCount);
    grid.setPageIndex(pageIndex);
    grid.setPageSize(pageSize);

    grid.setData(arr);
}

// 监听分页前事件，阻止后自行设置当前数据和分页信息
grid.on("beforeload", function (e) {
    e.cancel = true;
    pageIndex = e.data.pageIndex, pageSize = e.data.pageSize;
    getArchivesList(pageIndex + 1, pageSize, searchName);
    fillData(pageIndex, pageSize, dataResult, grid);
});



function refresh(content) {
    mini.showTips({
        content: content,
        state: 'success',
        x: 'Center',
        y: 'Top',
        timeout: 3000
    });
    getArchivesList(pageIndex + 1, pageSize, searchName);
    fillData(pageIndex, grid.getPageSize(), dataResult, grid);
}



//回车键
function onKeyEnter(e) {
    search();
}

//搜索
function search() {
    searchName = mini.get("searchName").getValue();
    if (searchName !== '') {
        getArchivesList(pageIndex + 1, pageSize, searchName);
        fillData(0, grid.getPageSize(), dataResult, grid);
    } else {
        searchName = '';
        getArchivesList(pageIndex + 1, pageSize, searchName);
        fillData(0, grid.getPageSize(), dataResult, grid);
    }
}


//编辑
function edit() {
    var row = grid.getSelected();
    if (row) {
        mini.open({
            url: BaseUrl() + "/docker/archives/addUpdate.html",
            title: "编辑档案", width: 600, height: 400,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "edit", archid: row.archid };
                iframe.contentWindow.SetData(data);

            },
            ondestroy: function (action) {
                
                //var iframe = this.getIFrameEl();
                grid.reload();

            }
        });
    } else {
        mini.showTips({
            content: "请选择一条记录",
            state: 'danger',
            x: 'Center',
            y: 'Top',
            timeout: 2000
        });
    }

}

//添加
function add() {
    mini.open({
        targetWindow: window,

        url: BaseUrl() + "/docker/archives/addUpdate.html",
        title: "添加档案", width: 600, height: 400,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { action: "add" };
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
            grid.reload();
        }
    });
}

//删除
function remove() {
    var rows = grid.getSelecteds();
    if (rows.length == 0) {
        mini.showTips({
            content: "请选择一条记录",
            state: 'danger',
            x: 'Center',
            y: 'Top',
            timeout: 2000
        });
    } else {
        mini.confirm("确定删除？", "确定？",
            function (action) {
                if (action == "ok") {
                    var archids = [];
                    for (var i = 0; i < rows.length; i++) {
                        archids.push(rows[i].archid);
                    }
                        
                    $.ajax({
                        url: GatewayUrl() + '/archives/delete',
                        type: 'post',
                        contentType: "application/json",
                        data: JSON.stringify(archids),
                        headers: {
                            "Authorization": localStorage.getItem("token")
                        },
                        success: function (dt) {
                            if (dt.code === 200) {
                                refresh('删除成功');
                            }
                        }
                    })
                }
            }
        );
    }
}


//自定义单元格内容、样式
grid.on("drawcell", function (e) {
    var record = e.record,
        column = e.column,
        field = e.field,
        value = e.value;



    //将性别文本替换成图片
    if (field == "sex") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-boy'></span>"
        } else {
            e.cellHtml = "<span class='icon-female'></span>"
        }
    }

    //是否高血压
    if (field == "isBlood") {
        if (e.value == 1) {
            e.cellHtml = "是";
            e.cellStyle = "color:red;font-weight:bold;";
        } else {
            e.cellHtml = "否";
            e.cellStyle = "color:green;font-weight:bold;";
        }
    }

    //是否糖尿病
    if (field == "isSuger") {
        if (e.value == 1) {
            e.cellHtml = "是";
            e.cellStyle = "color:red;font-weight:bold;";
        } else {
            e.cellHtml = "否";
            e.cellStyle = "color:green;font-weight:bold;";
        }
    }



})